package com.example.opengldemo.simpleGame.addRigidBody;


import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.CompoundShape;
import com.bulletphysics.collision.shapes.CylinderShapeZ;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.linearmath.Transform;
import com.example.opengldemo.simpleGame.MatrixState.MatrixState3D;
import com.example.opengldemo.simpleGame.catcherFun.MySurfaceView;
import com.example.opengldemo.simpleGame.object.BNAbstractDoll;
import com.example.opengldemo.simpleGame.object.LoadedObjectVertexNormalTexture;
import com.example.opengldemo.simpleGame.util.RigidBodyHelper;

import javax.vecmath.Vector3f;

import static com.example.opengldemo.simpleGame.constant.SourceConstant.*;

public class Camera extends BNAbstractDoll {
	int texId;
	DiscreteDynamicsWorld dynamicsWorld;
	Vector3f position;
	CollisionShape[] camerashape=new CollisionShape[5];

	MySurfaceView mv;
	public Camera(int texId, DiscreteDynamicsWorld dynamicsWorld,
				  LoadedObjectVertexNormalTexture lovo, Vector3f position, int bianhao)
	{
		this.texId=texId;
		this.dynamicsWorld=dynamicsWorld;
		this.lovo=lovo;
		this.position=position;
		this.bianhao=bianhao;
		initRigidBodys();
	}
	public void initRigidBodys()
	{

		camerashape[0]=new BoxShape(new Vector3f(Camerabodyx,Camerabodyy,Camerabodyz));

		camerashape[1]=new CylinderShapeZ(new Vector3f(CameraR,CameraH,CameraR));

		camerashape[2]=new BoxShape(new Vector3f(CameraTopx,CameraTopy,CameraTopz));
		
		camerashape[3]=addChild(camerashape);
		RigidBodydoll= RigidBodyHelper.addRigidBody(1,camerashape[3],position.x,position.y,position.z,dynamicsWorld,false);
		
		
	} 

  	public CompoundShape addChild(CollisionShape[] shape)
  	{
  		CompoundShape comShape=new CompoundShape();
  		Transform localTransform = new Transform();
  		
  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(0,Camerabodyy,-Camerabodyz-0.08f));
  		comShape.addChildShape(localTransform, shape[0]);
  		
  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(-0.06f,CameraR,CameraH/2-0.03f));
  		comShape.addChildShape(localTransform, shape[1]);
  		
  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(-0.022f,Camerabodyy*2+CameraTopy,-Camerabodyz-0.08f));
  		comShape.addChildShape(localTransform, shape[2]);
  		
  		return comShape;
  	}
  	public void drawSelf()
	{

  		
	  		MatrixState3D.pushMatrix();
			Transform trans=RigidBodydoll.getMotionState().getWorldTransform(new Transform());
			MatrixState3D.translate(trans.origin.x,trans.origin.y-speed, trans.origin.z);
			trans.getOpenGLMatrix(MatrixState3D.getMMatrix());
			
			MatrixState3D.pushMatrix();
			MatrixState3D.scale(Camerabl,Camerabl,Camerabl);
			lovo.drawSelf(texId);
			MatrixState3D.popMatrix();
			MatrixState3D.popMatrix();
	}

}
